<?php

namespace libs;

use config\parser as parser;
use libs\registry as Registry;

class controller {
    
    public $_cache;
    public $_db;
    public $_view;
    public $_logReq = false;
    public $options;
    
    
    public function __construct($options) 
    {
        
        if (is_array($options) || is_object($options))
        {
            foreach ($options as $key => $value)
            {
                $key = ucfirst($key);
                $method = "set{$key}";
                $this->$method($value);

            }
        }
        
        Registry::set('database',database::query());
        Registry::set('SQL',database::connect());
        
        $this->options = $options;
    }
    
    private function setDbconfig($config)
    {
        list($name,$extension) = explode('.', $config);
        
        try{
            if(in_array($extension, parser::getArray()))        
                $config = parser::$extension($name);
            else
                throw new \Exception ("Nie obsługujemy rozszerzenia <b>.{$extension}</b> w configu");
        }
        
        catch(\Exception $e) {
            echo $e->getMessage();
            die();
        }
        
        database::$_config = ArrayMethods::toObject($config['database']);
    }
    
    
    public function __call($name, $arguments) 
    {
          $getMatches = StringMethods::match($name, "^get([a-zA-Z0-9]+)$");
          
            if (sizeof($getMatches) > 0)
            {
                $normalized = lcfirst($getMatches[0]);
                $property = "_{$normalized}";
                
                if (property_exists($this, $property))
                {
                    if (isset($this->$property))
                    {
                        return $this->$property;
                    }
                    
                    return null;
                }
            } 
            
            $setMatches = StringMethods::match($name, "^set([a-zA-Z0-9]+)$");
            if (sizeof($setMatches) > 0)
            {
                
                $normalized = lcfirst($setMatches[0]);
                $property = "_{$normalized}";
                
                if (property_exists($this, $property))
                {   
                    $this->$property = $arguments[0];
                    return $this;
                }
            }
    }
    
    
    public function error404()
    {
        include 'error.php';
        die();
    }
    
    
    public function __get($name) 
    {
        $function = 'get'.  ucfirst($name);
        return $this->$function($name);
    }
    
    
    public function __set($name, $value) 
    {    
        $function = 'set'.  ucfirst($name);
        return $this->$function($name,$value);
    }
    
    public static function slugify($text)
    { 
      // replace non letter or digits by -
      $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

      // trim
      $text = trim($text, '-');

      // transliterate
      $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

      // lowercase
      $text = strtolower($text);

      // remove unwanted characters
      $text = preg_replace('~[^-\w]+~', '', $text);

      if (empty($text))
      {
        return 'n-a';
      }

      return $text;
    }
}

?>
